package com.furniture.sql.manage;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

import com.furniture.sql.DaoMaster;
import com.furniture.sql.DaoSession;
import com.furniture.sql.Good;
import com.furniture.sql.GoodDao;
import com.furniture.ui.utils.data.QiNiuUpLoad;

/**
 * Created by Bob on 2016/3/21.
 */
public class GoodManage {

    private Context mContext;
    private GoodDao mGoodDao;
    private SQLiteDatabase db;
    private static GoodManage mMe;
    private List<Good> goodList;

    public void setGoodList(List<Good> goodList) {
        this.goodList = goodList;
    }

    public static GoodManage newInstance(Context ctx) {
        if (mMe == null) {
            mMe = new GoodManage(ctx);
        }
        return mMe;
    }

    public GoodManage(Context ctx) {
        this.mContext = ctx;
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(ctx, "good_list_db", null);
        db = helper.getWritableDatabase();
        DaoMaster daoMaster = new DaoMaster(db);
        DaoSession daoSession = daoMaster.newSession();
        mGoodDao = daoSession.getGoodDao();
    }

    public void addItem(Good item){
        Good good = new Good();
        good.setId(item.getId() == null ? 0 : item.getId());
        good.setGoodMsg(item.getGoodMsg() == null ? "" : item.getGoodMsg());
        good.setGoodPic(item.getGoodPic() == null ? "" : item.getGoodPic());
        good.setIcon(item.getIcon() == null ? "" : item.getIcon());
        good.setGoodUrl(item.getGoodUrl() == null ? "" : item.getGoodUrl());
        good.setNickName(item.getNickName() == null ? "" : item.getNickName());
        good.setIsLike(item.getIsLike() == null ? false : item.getIsLike());
        good.setLikeNum(item.getLikeNum() == null ? 0 : item.getLikeNum());
        good.setUserId(item.getUserId() == null ? 0 : item.getUserId());
        mGoodDao.insertOrReplace(item);
    }

    public void delete(long item){
        long i = getGoodDaoNum();
        Log.e("isDelete", String.valueOf(i));
        Good goodItem = mGoodDao.load(i-item);
        if (goodItem != null){
            mGoodDao.delete(goodItem);
            new QiNiuUpLoad("goodList.json", goodList);
        }
    }

    public List<Good> showGoodList(){
        List list = mGoodDao.queryBuilder().orderDesc(GoodDao.Properties.Id).list();
        List<Good> arrayList = new ArrayList<Good>();
        for (Object object : list){
            Good item = (Good) object;
            arrayList.add(item);
        }
        return arrayList;
    }

    public List<String> showGoodListUrl(){
        List list = mGoodDao.queryBuilder().orderDesc(GoodDao.Properties.Id).list();
        List<String> arrayList = new ArrayList<String>();
        for (Object object : list){
            Good item = (Good) object;
            arrayList.add(item.getGoodUrl());
        }
        return arrayList;
    }

    public List<String> showLikeListUrl(){
        List list = mGoodDao.queryBuilder().orderDesc(GoodDao.Properties.Id).list();
        List<String> arrayList = new ArrayList<String>();
        for (Object object : list){
            Good item = (Good) object;
            if (item.getIsLike()){
                arrayList.add(item.getGoodUrl());
            }
        }
        return arrayList;
    }

    public long getGoodDaoNum(){
        return mGoodDao.count();
    }
}
